package com.kingyea.app.market.user.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kingyea.app.market.client.UserServiceClient;
import com.kingyea.app.market.supplierAllocation.service.SupplierAllocationService;
import com.kingyea.app.market.user.service.UserService;
import com.kingyea.app.market.utils.page.PageInfo;
import com.kingyea.mobilepolice.user.bean.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author
 * @version v1.0.0
 * @description User 控制层
 * @date Created in 2018-02-09 17:37:33
 */
@RequestMapping(value = "/user")
@RestController
public class UserController {

    private static final Logger LOGGER = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;
    @Autowired
    private UserServiceClient userServiceClient;
    @Autowired
    private SupplierAllocationService supplierAllocationService;

    /**
     * 查询全部
     *
     * @param
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public Object list() throws Exception {
        return userService.list(null);
    }

    /**
     * 通过ID查找对象
     *
     * @return
     */
    @RequestMapping(value = "/{idcard}", method = RequestMethod.GET)
    public User detail(@PathVariable String id) throws Exception {
        return userService.getById(id);
    }

    /**
     * 根据条件查询
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/getUserList", method = RequestMethod.POST)
    public Object conditionQuery(@RequestBody Map<String, Object> user) throws Exception {
        return userService.list(user);
    }

    /**
     * 创建
     *
     * @param user
     * @return
     */
    @RequestMapping(value = "/addUser", method = RequestMethod.POST)
    public User create(@RequestBody User user) throws Exception {
        return userService.save(user);
    }

    /**
     * 更新
     *
     * @param user
     * @return
     */
    @RequestMapping(value = "/editUser", method = RequestMethod.POST)
    public Object update(@RequestBody User user) throws Exception {
        return userService.update(user);
    }

    /**
     * 删除
     *
     * @return
     */
    @RequestMapping(value = "/deleteUser", method = RequestMethod.POST)
    public Object delete(@RequestBody User user) throws Exception {
        return userService.deleteById(user);
    }

    /**
     * 关联查询  找出供应商未有权限的用户
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getListBySupplier", method = RequestMethod.POST)
    public Object getListBySupplier(@RequestBody Map<String, Object> obj) throws Exception {
        List<String> supplierCodeList = new ArrayList<String>();
        List<Object> listNoPage = supplierAllocationService.listNoPage(obj);
        for (Object object : listNoPage) {
            JSONObject sup = JSON.parseObject(JSON.toJSONString(object));
            if (sup.get("loginAccount") != null) {
                supplierCodeList.add(sup.get("loginAccount").toString());
            }
        }
        obj.put("pageNo", obj.get("pageNum"));
        obj.put("userIdHave", supplierCodeList);
        Object object = userServiceClient.getUserList(obj);
        JSONObject data = JSON.parseObject(JSON.toJSONString(object));
        JSONObject results = JSON.parseObject(JSON.toJSONString(data.get("data")));
        List<User> userList = (List<User>) results.get("list");
        PageInfo p = new PageInfo();
        p.setList(userList);
        if (userList.size() > 0) {
            p.setPageNum(Integer.parseInt(results.get("pageNo").toString()) == 0 ? 1 : Integer.parseInt(results.get("pageNo").toString()));
            p.setPageSize(Integer.parseInt(results.get("pageSize").toString()));
            p.setTotal(Long.parseLong(results.get("totalRecord").toString()));
        } else {
            p.setPageNum(Integer.parseInt(obj.get("pageNo").toString()));
            p.setPageSize(Integer.parseInt(obj.get("pageSize").toString()));
            p.setTotal(userList.size());
        }
        return p;
    }
}
